package com.jason.quectel0805.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jason.quectel0805.R;
import com.jason.quectel0805.entities.Conversation;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author：Jason
 * @date：2022/8/9 15:21
 * @email：1129847330@qq.com
 * @description:
 */
public class WeChatHomeAdapter extends RecyclerView.Adapter<WeChatHomeAdapter.WechatHomeViewHolder> {
    private static final String TAG = "jasonwan";
    private List<Conversation> list = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;

    public void setList(List<Conversation> list) {
        if (list != null) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public WeChatHomeAdapter(List<Conversation> list, Context context) {
        if (list != null) {
            this.list.addAll(list);
        }
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @NonNull
    @Override

    public WechatHomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: ");
        View view = inflater.inflate(R.layout.item_wechat_home, null);
        WechatHomeViewHolder viewHolder = new WechatHomeViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WechatHomeViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: position=" + position);
        Conversation conersation = list.get(position);
        holder.tvNickname.setText(conersation.getNickname());
        holder.tvContent.setText(conersation.getChatContent());
        Glide.with(context).load(conersation.getAvatar()).into(holder.ivAvatar);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class WechatHomeViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvNickname;
        TextView tvContent;

        public WechatHomeViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_home_avatar);
            tvNickname = itemView.findViewById(R.id.tv_home_nickname);
            tvContent = itemView.findViewById(R.id.tv_home_chat_content);
        }
    }
}